<?php 
include 'Interfaces/FormElementInterface.php';

class Input implements FormElementInterface
{
	private $dt;
	private $dd;
	private $element;
	
	public function __construct($name = '', $label = '', $value = '', $properties = '')
	{
		$this->element = null;
		$this->dt = ($label) ? $this->crateDT( $name, $label ) : '';
		$this->dd = ($name) ? $this->crateDD( $name, $value, $properties ) : '';
	}
	
	public function __toString()
	{
		return $this->dt . $this->dd;
	}
	
	public function getType()
	{
		return isset($this->element) ? $this->element->type : false;
	}
	
	public function getName()
	{
		return isset($this->element) ? $this->element->name : false;
	}
	
	public function getElement()
	{
		return isset($this->element) ? $this->element : false;
	}
	
	protected function crateDT($name, $label)
	{
		$lb = new ElementXml( 'label' );
		$lb->for = $name;
		$lb->addElement( $label );
		
		$dt = new ElementXml( 'dt' );
		$dt->id = $name . '-label';
		$dt->class = $name . '-label';
		$dt->addElement($lb);
		
		return $dt;
	}
	
	public function crateDD($name, $value, $properties)
	{
		$element = $this->createElement($name, $value, $properties);
		
		$dd = new ElementXml( 'dd' );
		$dd->id = $name . '-object';
		$dd->class = $name . '-object';
		$dd->addElement($element);
		
		return $dd;
	}
	
	public function createElement($name, $value, $properties)
	{
		$element = new ElementXml( 'input' );
		$element->type = 'text';
		$element->id = $name;
		$element->class = $name;
		$element->name = $name;
		$element->value = $value;
		if($properties) foreach ($properties as $key => $val) $element->$key = $val;
		
		$this->element = $element;
		
		return $element;
	}
}